package com.tommy.producer.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

@RestController
public class MessageController {

    private final StreamBridge streamBridge;

    @Autowired
    public MessageController(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }

    @PostMapping("/send") // 改为POST请求
    public String sendMessage(@RequestBody User user) { // 接收JSON对象
        boolean send = streamBridge.send("sendMessage-out-0", user);
        return send ? "success: " + user : "failed";
    }

    static class User implements Serializable   {
        String name;
        int age;

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }


    }
}
